/***************************************************************************
 * Copyright (C) 2013 Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * CommonButtons.cpp
 *
 * for SaveGame Manager GX
 ***************************************************************************/
#include "CommonButtons.hpp"
#include "../Controls/Application.h"
#include "../DeviceControls/DeviceHandler.hpp"

CommonButtons::CommonButtons()
	: GuiFrame(400, 150)
{
	action = -1;
	deleted = false;
	installed = false;
	eNandCount = 0;
	emu1 = false;
	emu2 = false;
	emu3 = false;
	
	SetAlignment(ALIGN_CENTER | ALIGN_BOTTOM);
	SetPosition(-6, -30);
	
	actionButtons = NULL;
	
	confirmButtons = new ManageButtons(tr( "Extract to:" ), 0, tr( "OK" ), tr( "Cancel" ));
	confirmButtons->ButtonClicked.connect(this, &CommonButtons::OnConfirmClick);
	
	succesButtons = new ManageButtons(tr("Succesfully extracted"), 0, tr( "OK" ));
	succesButtons->ButtonClicked.connect(this, &CommonButtons::OnResultClick);
	
	errorButtons = new ManageButtons(tr( "Error:" ), 0, tr( "OK" ));
	errorButtons->ButtonClicked.connect(this, &CommonButtons::OnResultClick);
	
	cancelButtons = new ManageButtons(tr("Extract cancelled"), 0, tr( "OK" ));
	cancelButtons->ButtonClicked.connect(this, &CommonButtons::OnResultClick);
}

CommonButtons::~CommonButtons()
{
	if(actionButtons)
		delete actionButtons;
	delete confirmButtons;
	delete succesButtons;
	delete errorButtons;
	delete cancelButtons;
}

void CommonButtons::OnResultClick(ManageButtons *buttons, int choice UNUSED)
{
	if(deleted)
		EntryDeleted();
	deleted = false;
	
	HideButtons(buttons);
	Cancel();
}

void CommonButtons::Cancel()
{
	ShowButtons(actionButtons);
	
	BlockButtons(false);
}

void CommonButtons::Confirm()
{
	confirmButtons->SetMessage(Settings.TmpPath);
	
	ShowButtons(confirmButtons);
}

void CommonButtons::HideButtons(ManageButtons *buttons)
{
	buttons->SetEffect(EFFECT_FADE, -20);
	while(buttons->IsAnimated())
		Application::Instance()->updateEvents();
	Remove(buttons);
}

void CommonButtons::ShowButtons(ManageButtons *buttons)
{
	buttons->SetEffect(EFFECT_FADE, 20);
	Append(buttons);
	while(buttons->IsAnimated())
		Application::Instance()->updateEvents();
}

void CommonButtons::SetENandValues()
{
	for(int ewii = EWII1; ewii <= EWII3; ++ewii)
	{
		if(DeviceHandler::Instance()->IsInserted(ewii))
		{
			if(ewii == EWII1)
			{
				emu1 = true;
				eNandCount++;
			}
			else if(ewii == EWII2)
			{
				emu2 = true;
				eNandCount++;
			}
			else if(ewii == EWII3)
			{
				emu3 = true;
				eNandCount++;
			}
		}
	}
}
